package com.bskj.bms.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.bms.service.IBmsIndexService;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.ParkingLotCustomer;
import com.bskj.common.dao.BmsUserDao;
import com.bskj.common.dao.ParkingLotCustomerDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.common.utils.EncryptUtil;
import com.bskj.common.utils.StringUtils;

@Service
public class BmsIndexService extends BaseServiceImpl<BmsUser> implements IBmsIndexService{
	@Autowired
	private BmsUserDao bmsUserDao;
	
	@Autowired
	private ParkingLotCustomerDao parkingLotCustomerDao;
	
	@Override
	public BaseDao<BmsUser> getBaseDao() {
		return bmsUserDao;
	}

	@Override
	public Map<String, Object> modify(String oldPwd,String newPwd,String newPwds,Integer id) {
		Map<String, Object> resultMap=new HashMap<>();
		BmsUser bmsUser=bmsUserDao.get(id);
		if(StringUtils.isBank(oldPwd)||StringUtils.isBank(newPwd)||StringUtils.isBank(newPwds)){
			resultMap.put("success",false);
			resultMap.put("meg","不能有空");
		}else if(!newPwd.equals(newPwds)){
			resultMap.put("success",false);
			resultMap.put("meg","新密码两次输入不一致");
		}else if (!EncryptUtil.getMD5(oldPwd).equals(bmsUser.getPassword())){
			resultMap.put("success",false);
			resultMap.put("meg","当前密码输入有错误");
		}else if(oldPwd.equals(newPwds)){
			resultMap.put("success",false);
			resultMap.put("meg","新密码与当前密码一样");
		}else {
			try {
				bmsUser.setPassword(EncryptUtil.getMD5(newPwd));
				bmsUser.setUpdateTime(new Date());
				bmsUserDao.update(bmsUser);
				resultMap.put("success",true);
				resultMap.put("meg","修改成功");
			} catch (Exception e) {
				resultMap.put("success",false);
				resultMap.put("meg","修改失败，系统繁忙");
				e.printStackTrace();
				return resultMap;
			}
		}
		return resultMap;
	}

	@Override
	public String parkName(BmsUser bmsUser) throws Exception {
		ParkingLotCustomer parkingLotCustomer= parkingLotCustomerDao.get(bmsUser.getCustId());
		return parkingLotCustomer.getParkingName();
	}
}
